12. Exercise: Which Match
Which Match
Let’s say we want to delete some data. From our app’s UI code we call the delete function on a ContentResolver, with the following URI passed in:
int id = 4;
String idString = "" + id;
Uri uri = TaskContract.TaskEntry.CONTENT_URI;
uri = uri.buildUpon().appendPath(idString).build();
getContentResolver().delete(uri, null, null);
Assume that the URI matcher you are using is the one from the previous video, namely:
public static UriMatcher buildUriMatcher() {
// Initialize a UriMatcher with no matches by passing in NO_MATCH to the constructor
UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
/*
All paths added to the UriMatcher have a corresponding int.
For each kind of uri you may want to access, add the corresponding match with addURI.
The two calls below add matches for the task directory and a single item by ID.
*/
uriMatcher.addURI(TaskContract.AUTHORITY, TaskContract.PATH_TASKS, TASKS);
uriMatcher.addURI(TaskContract.AUTHORITY, TaskContract.PATH_TASKS + "/#", TASK_WITH_ID);
return uriMatcher;
}